package com.abbas.smartCity.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;

import com.abbas.smartCity.App;
import com.abbas.smartCity.R;
import com.abbas.smartCity.adapter.BannerImageAdapter2;
import com.abbas.smartCity.base.BaseActivity;
import com.abbas.smartCity.ui.kt.KLoginActivity;
import com.youth.banner.Banner;
import com.youth.banner.indicator.CircleIndicator;
import com.youth.banner.listener.OnPageChangeListener;

import java.util.Arrays;
import java.util.List;

/**
 * 欢迎页面
 */
@SuppressLint("CustomSplashScreen")
public class SplashActivity extends BaseActivity {

    private Banner<Integer, BannerImageAdapter2<Integer>> banner;//轮播器
    private Button netSetBt;//网络设置按钮
    private Button intoBt;//跳转登录按钮

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected String setCustomTitle() {
        return null;
    }

    @Override
    protected void initView() {
        banner = findViewById(R.id.banner);
        netSetBt = (Button) findViewById(R.id.net_set_bt);
        intoBt = (Button) findViewById(R.id.into_bt);
    }

    @Override
    protected void initData() {
        //检查是否是第一次进入，如第一次，此值默认true
        final boolean isFirst = App.getSharedPreferences()
                .getBoolean("isFirst", true);

        //检查完毕后将值设置为false，此后将不再进入此页面
        App.getSharedPreferences()
                .edit()
                .putBoolean("isFirst", false)
                .apply();

        //如为false，直接跳转值登录页面
        if (!isFirst) {
            //跳转到登录界面
            startActivity(LoginActivity.class);
            SplashActivity.this.finish();
        }
        //初始化图片
        final List<Integer> images = Arrays.asList(R.drawable.smartcity1, R.drawable.smartcity2, R.drawable.smartcity3, R.drawable.smartcity4, R.drawable.smartcity5);
        //设置数据，配置轮播器
        banner.setAdapter(new BannerImageAdapter2<>(images))//适配器
                .isAutoLoop(false)//关闭自动滑动
                .setIndicator(new CircleIndicator(SplashActivity.this))//设置指示器
                .addOnPageChangeListener(new OnPageChangeListener() {//添加页面滑动监听
                    @Override
                    public void onPageScrolled(int i, float v, int i1) {
                        //第五页显示设置按钮与进入按钮，其他页面隐藏
                        if (i == 4) {
                            netSetBt.setVisibility(View.VISIBLE);
                            intoBt.setVisibility(View.VISIBLE);
                        } else {
                            netSetBt.setVisibility(View.GONE);
                            intoBt.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onPageSelected(int i) {

                    }

                    @Override
                    public void onPageScrollStateChanged(int i) {

                    }
                })
                .start();


    }

    @Override
    protected void initEvent() {
        //网络设置按钮点击
        netSetBt.setOnClickListener((view) -> {
            //新建一个输入框
            final EditText editText = new EditText(SplashActivity.this);
            editText.setHint("输入格式：192.168.0.1:10001");
            //新建一个弹窗
            new AlertDialog.Builder(SplashActivity.this)
                    .setTitle("端口IP设置")
                    .setView(editText)//在弹窗中设置输入框
                    .setNegativeButton("取消", (dialogInterface, i) -> dialogInterface.dismiss())//取消按钮
                    .setPositiveButton("确定", (dialogInterface, i) -> {//确定按钮
                        //保存IP地址到SP
                        App.getSharedPreferences()
                                .edit()
                                .putString("port", editText.getText().toString())
                                .apply();
                        //设置地址
                        App.setBaseUrl(App.getSharedPreferences().getString("port", ""));
                        //初始化Retrofit
                        App.createApi();
                        Toast.makeText(SplashActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
                        dialogInterface.dismiss();
                    })
                    .create()
                    .show();
        });

        //网络设置按钮
        intoBt.setOnClickListener((view) -> {
            //判断是否是空端口，空端口则不予进入主页
            if (TextUtils.equals(App.getBaseUrl(), "http://")) {
                Toast.makeText(SplashActivity.this, "请先输入端口与IP", Toast.LENGTH_SHORT).show();
            } else {
                startActivity(new Intent(SplashActivity.this, LoginActivity.class));
            }
        });
    }
}